<?php
namespace cms\modules\backend\controllers;

use Yii;
use cms\controllers\AuthController;
use app\models\ContentCategoryAttr;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;


class CategoryAttrController extends AuthController
{
    /*分类字段列表*/
    public function actionAttrList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        if(isset($this->get['pageSize'])){
            $pageSize = intval($this->get['pageSize']?$this->get['pageSize']:100);
        }else{
            $pageSize = Tool::getPageSize();
        }

        $contentCategoryAttr = new ContentCategoryAttr;
        $count = $contentCategoryAttr::find()->where('app_id = :app_id', [':app_id' => $this->app_id])->count();
        $contentCategoryAttrList = $contentCategoryAttr::find()
                                ->where('app_id = :app_id', [':app_id' => $this->app_id])
                                ->offset($pageSize*($currPage-1))->limit($pageSize)
                                ->orderBy([
                                    'attr_id' => SORT_DESC,
                                ])
                                ->asArray()->all();
        $res = array(
            'list' => $contentCategoryAttrList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加分类字段*/
    public function actionAddAttr()
    {
        if(Yii::$app->request->isPost){
            $contentCategoryAttr = new ContentCategoryAttr;
            if($contentCategoryAttr->addAttr($this->post, $this->app_id)){
                $backInfo = [0, '添加APP分类字段成功'];//返回信息
            }else{
                if($contentCategoryAttr->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $contentCategoryAttr->getErrors();//异常信息
                    if(array_key_exists("attr_name_repeat", $exception)){
                        $backInfo = ErrMap::getError(10005, '此字段名已存在');//返回信息
                    };
                    if(array_key_exists("attr_enname_repeat", $exception)){
                        $backInfo = ErrMap::getError(10005, '此英文字段名已存在');//返回信息
                    };
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*修改分类字段的值*/
    public function actionModifyAttr()
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $contentCategoryAttr = ContentCategoryAttr::find()
                                ->where('attr_id = :id', [':id' => $id])
                                ->asArray()->one();
                if(!is_null($contentCategoryAttr)){
                    // P($contentCategoryAttr);
                    return Tool::showRes([0, '获取成功'], $contentCategoryAttr);
                }else{
                    return Tool::showRes(ErrMap::getError(10010));
                };
            }
        }


        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['ContentCategoryAttr']['id'])?intval($this->post['ContentCategoryAttr']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $contentCategoryAttr = new ContentCategoryAttr;
                if($contentCategoryAttr->modifyAttr($this->post, $id)){
                    $backInfo = [0, '修改分类字段成功'];//返回信息
                }else{
                    if($contentCategoryAttr->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $contentCategoryAttr->getErrors();//异常信息
                        if(array_key_exists("attr_name_repeat", $exception)){
                            $backInfo = ErrMap::getError(10005, '此字段名已存在');//返回信息
                        };
                        if(array_key_exists("attr_enname_repeat", $exception)){
                            $backInfo = ErrMap::getError(10005, '此英文字段名已存在');//返回信息
                        };
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*是否启用1-不-2启动（状态操作）*/
    public function actionSetState()
    {
        if(Yii::$app->request->isPost){
            $id = isset($this->post['id'])?$this->post['id']:'';
            $contentCategoryAttr = ContentCategoryAttr::find()->select('state')
                                ->where('attr_id = :id', [':id' => $id])->one();
            // P($contentCategoryAttr);
            if(!is_null($contentCategoryAttr)){
                $contentCategoryAttr->updateAll(['state' => Tool::changeBool($this->post['state']),], 'attr_id = :id', [':id' => $id]);
                $backInfo = [0, '设置自定义字段状态成功'];//返回信息
            };
        }

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*删除分类字段*/
    public function actionDeleteAttr()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $contentCategoryAttr = new ContentCategoryAttr;
            if($contentCategoryAttr->deleteAll('attr_id = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除分类字段成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除分类字段失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));

    }


}
